using System.Collections;
using UnityEngine;

public class PhysicalSupportTools : MonoBehaviour
{
	[SerializeField]
	private PhysicalSupportType m_SupportType;

	private IPhysicalSupport m_Support;

	private void FixedUpdate()
	{
		m_Support.FixedCall();
	}

	public static PhysicalSupportTools AngularVelocityCorrectionTo(GameObject o, Hashtable table)
	{
		AngularVelocityCorrection angularVelocityCorrection = new AngularVelocityCorrection();
		angularVelocityCorrection.Init(table);
		return AddSupportTools(o, PhysicalSupportType.AngularVelocityCorrection, angularVelocityCorrection);
	}

	public static PhysicalSupportTools AngularVelocityCorrectionTo(GameObject o, Rigidbody rb, float r)
	{
		AngularVelocityCorrection angularVelocityCorrection = new AngularVelocityCorrection();
		angularVelocityCorrection.Init(Table("Rigidbody", rb, "Radius", r));
		return AddSupportTools(o, PhysicalSupportType.AngularVelocityCorrection, angularVelocityCorrection);
	}

	public static PhysicalSupportTools MaxSpeedLimitTo(GameObject o, Hashtable table)
	{
		MaxSpeedLimit maxSpeedLimit = new MaxSpeedLimit();
		maxSpeedLimit.Init(table);
		return AddSupportTools(o, PhysicalSupportType.MaxSpeedLimit, maxSpeedLimit);
	}

	public static PhysicalSupportTools MaxSpeedLimitTo(GameObject o, float maxSpeed)
	{
		if (o.GetComponent<Rigidbody>() == null)
		{
			UnityEngine.Debug.LogError("can not found the rigidbody at the gameobject : " + o);
			return null;
		}
		MaxSpeedLimit maxSpeedLimit = new MaxSpeedLimit();
		maxSpeedLimit.Init(Table("Rigidbody", o.GetComponent<Rigidbody>(), "MaxSpeed", maxSpeed));
		return AddSupportTools(o, PhysicalSupportType.MaxSpeedLimit, maxSpeedLimit);
	}

	public static PhysicalSupportTools PhysicalDragTo(GameObject o, Hashtable table)
	{
		PhysicalDrag physicalDrag = new PhysicalDrag();
		physicalDrag.Init(table);
		return AddSupportTools(o, PhysicalSupportType.PhysicalDrag, physicalDrag);
	}

	public static PhysicalSupportTools PhysicalDragTo(GameObject o, float velocityDrag, float angularVelocityDrag)
	{
		if (o.GetComponent<Rigidbody>() == null)
		{
			UnityEngine.Debug.LogError("can not found the rigidbody at the gameobject : " + o);
			return null;
		}
		PhysicalDrag physicalDrag = new PhysicalDrag();
		physicalDrag.Init(Table("Rigidbody", o.GetComponent<Rigidbody>(), "VelocityDrag", velocityDrag, "AngularVelocityDrag", angularVelocityDrag));
		return AddSupportTools(o, PhysicalSupportType.PhysicalDrag, physicalDrag);
	}

	private static PhysicalSupportTools AddSupportTools(GameObject o, PhysicalSupportType supportType, IPhysicalSupport support)
	{
		PhysicalSupportTools physicalSupportTools = o.GetComponent<PhysicalSupportTools>();
		if (!physicalSupportTools || physicalSupportTools.m_SupportType != supportType)
		{
			physicalSupportTools = o.AddComponent<PhysicalSupportTools>();
			physicalSupportTools.m_SupportType = supportType;
		}
		physicalSupportTools.m_Support = support;
		return physicalSupportTools;
	}

	public static void Remove(GameObject o, PhysicalSupportType supportType)
	{
		PhysicalSupportTools[] components = o.GetComponents<PhysicalSupportTools>();
		if (components == null && components.Length == 0)
		{
			return;
		}
		int i = 0;
		for (int num = components.Length; i < num; i++)
		{
			if (components[i].m_SupportType == supportType)
			{
				UnityEngine.Object.Destroy(components[i]);
			}
		}
	}

	public static Hashtable Table(params object[] values)
	{
		int num = values.Length;
		if (num % 2 != 0)
		{
			UnityEngine.Debug.LogError("create hash table error. the number of params must be even number!");
			return null;
		}
		Hashtable hashtable = new Hashtable();
		for (int i = 0; i < num; i += 2)
		{
			if (!(values[i] is string))
			{
				UnityEngine.Debug.LogError("the key of the hashtable must be string");
				return null;
			}
			hashtable.Add(values[i], values[i + 1]);
		}
		return hashtable;
	}

	public static Vector3 CalculateAngularVelocity(float radius, Vector3 velocity)
	{
		return new Vector3(CalculateAngularVelocity(radius, velocity.z), CalculateAngularVelocity(radius, 0f), CalculateAngularVelocity(radius, 0f - velocity.x));
	}

	public static float CalculateAngularVelocity(float radius, float velocity)
	{
		return velocity / radius;
	}
}
